<?php

/**
 * 关键字控制器
 *
 * @author mengyuwang
 */

require_once 'CommonController.php';

class Admin_TestKeywordsController extends Admin_CommonController
{
    /**
     * 初始化函数
     * 
     * @return void
     */
    public function init()
    {
        parent::init();
        $this->_model = new Application_Model_TestKeywords();
        $this->_productModel = new Application_Model_PartnerProduct();       
        $this->view->title = "管理中心";
    }

    /**
     * 列表
     * 
     * @return void
     */
    public function indexAction()
    {          
        //获取当前页码
        $pageSize = 20;
        $pageNo = intval($this->_request->get('pageNo'));
        if (empty($pageNo)) {
            $pageNo = 1;
        }
        
        //设置URL模板
        $urls = array();
        $wheres = array();
        $keyword = trim($this->_request->get('keyword'));//关键字
        $this->view->keyword = $keyword;
        $type = intval($this->_request->get('type'));//类型
        $this->view->type = $type;
        $productCode = trim($this->_request->get('productCode'));//产品编号
        $this->view->productCode = $productCode;
        
        if ($keyword != '') {//按照关键字搜索
             $urls['keyword'] = $keyword;
             $wheres[] = "`value` LIKE '%" . addslashes($keyword) . "%'";
        }
        if ($type!=0) {//按照类型
            $urls['type'] = $type;
            $wheres[] = "`type` = $type";            
        }
        if ($productCode != '') {//按照产品编号搜索
             $urls['productCode'] = $productCode;
             $wheres[] = "`productCode` LIKE '" . addslashes($productCode). "'"; 
        }
        $urls['pageNo'] = '{page}';
        $urlTemplate = $this->view->projectUrl($urls);

        //获取总页数以及记录
       
        $where = count($wheres) > 0 ? ' WHERE ' . implode(' AND ', $wheres) . ' ' : '';      
        $sql = "SELECT * 
            FROM `{$this->_model->getTableName()}` 
            {$where} 
            ORDER BY `id` desc
        ";
        $dbPaginator = new Application_Model_DbPaginator($sql, $pageSize, $pageNo);
        $recordCount = $dbPaginator->getRecodCount();

        //获取分页html字符
        Zend_Loader::loadClass('Project_Paginator');
        
    
        $paginator = new Project_Paginator($recordCount, $pageSize, $pageNo);
        $paginator->urlTemplateContent = $urlTemplate;

        

        //分配view(分页处理后的记录集以及分页HTML字符)
        $this->view->pageString = $paginator->getPageString();
        
        $this->view->rows = $dbPaginator->getRows();
        $urls['pageNo'] = $pageNo;
        $urls['action']='index';
        $this->view->pageUrl = $this->view->projectUrl($urls);
     
        
        $this->view->productModel = $this->_productModel;
        $this->view->products = $this->_productModel->fetchAll();
        
    }
    

    /**
     * 添加动作
     * 
     * @return void
     */
    public function insertAction()
    {
    	$this->view->subtitle='添加关键字';
    	$this->view->products = $this->_productModel->fetchAll();
    	
        if ($this->_request->isPost()) {
            $row = array();
            $filter = new Zend_Filter_StripTags();
            
            $row['value'] = $filter->filter(trim($this->_request->getPost('value')));
            $row['type'] = intval($this->_request->getPost('type'));  
            $row['productCode'] = $filter->filter(trim($this->_request->getPost('productCode')))?$filter->filter(trim($this->_request->getPost('productCode'))):'*';
            $row['addUserName'] =$this->_currentUserRow['userName'];
            $row['addTime'] = date('Y-m-d H:i:s');
            
            if (strlen($row['value']) < 1) {
                echo $this->view->message('关键字长度不能小于1，请重新填写！') ;
                exit;
            }
            if ($row['type']==0) {
            	echo $this->view->message('必须选择类型，请重新填写。') ;
                exit; 
            }
            if ($this->_model->isExists($row)) {
                echo $this->view->message('已经存在相同的关键字类型和产品的组合，请重新填写。');
                exit; 
            }
            $this->_model->insert($row);
            echo $this->view->message('操作成功！', $this->view->projectUrl(array('action' => 'index'))) ;
            exit;
        }
        $this->renderScript('test-keywords/edit.php');
        
    }

    /**
     * 更新动作
     * 
     * @return void
     */
    public function updateAction()
    {
        $id = intval($this->_request->get('id'));
        $this->view->subtitle='修改关键字';
        if (empty($id)) {
             throw new Exception('Invalid params!');
        }
        $this->view->row = $this->_model->fetchRow("`id` = {$id}"); 
        $this->view->products = $this->_productModel->fetchAll();
        if ($this->_request->isPost()) {
            $row = array();
            $filter = new Zend_Filter_StripTags();
            $row['value'] = $filter->filter(trim($this->_request->getPost('value')));
            $row['type'] = intval($this->_request->getPost('type'));  
            $row['productCode'] = $filter->filter(trim($this->_request->getPost('productCode')))?$filter->filter(trim($this->_request->getPost('productCode'))):'*';
            
            if (strlen($row['value']) < 1) {
                echo $this->view->message('关键字长度不能小于1，请重新填写！') ;
                exit;
            }
            if ($row['type']==0) {
            	echo $this->view->message('必须选择类型，请重新填写。') ;
                exit; 
            }     
            if ($this->_model->isExists($row,$id)) {
                echo $this->view->message('已经存在相同的关键字类型和产品的组合，请重新填写。');
                exit; 
            }
            $this->_model->update($row, "`id` = {$id}");
            echo $this->view->message('操作成功！', $this->view->projectUrl(array('action' => 'index'))) ;
            exit;
        }
        
        $this->renderScript('test-keywords/edit.php');
    }

    /**
     * 删除动作
     * 
     * @return void
     */
    public function deleteAction()
    {
        if ($this->_request->isPost()) {
            $ids= $this->_request->getPost('selectId');
            
            if (is_array($ids)) {
                foreach($ids as $id) {
                   $this->_model->delete($id);
                }
            }
        }
        $backUrl = urldecode($this->_request->get('backUrl'));
        redirect($backUrl);       
        exit;
    }
    
}